package trinidad;

/**
 * Kontroler starcia
 */

public class GamePlay {
	/**
	 * channels
	 */
	//private ArtificalPlayer plaIn;
	public ArtificalPlayer plaOut;
	//private ArtificalPlayer plbIn;
	public ArtificalPlayer plbOut;
	
	/**
	 *  whose turn
	 */
	private int turn;
	
	/**
	 * game state
	 */
	private GameState state;
	
	/**
	 * player 1 type
	 */
	public int plaType;
	
	/**
	 * player 2 type
	 */
	public int plbType;
	/**
	 * players
	 */
	public static final int COMPUTER_MOVE = 0;
	public static final int HUMAN_MOVE = 1;
	public int whoseMove;
	
	public final static int HUMAN = 0;
	public final static int ALFABETAZABOJCA = 11;
	public final static int POWERBETA = 12;
	public final static int UNDERTTAKER = 15;
	public final static int MADDOG = 16;
	public final static int EMDA = 17;
	public final static int NET = 18;
	
	/* gamestates */
	public final static int NONE = 0;
	public final static int WINPLA = 1;
	public final static int WINPLB = 2;
	
	public int checkGameState() {
		if (state.piCount[0] < 3) return WINPLB;
		if (state.piCount[1] < 3) return WINPLA;
		for (int a = 2; a < 5; a++) {
			if (state.getState(2,a) == state.getState(3,a) && state.getState(3,a) == state.getState(4,a)) if (state.getState(4,a) > 0) return state.getState(4,a);
			if (state.getState(a,2) == state.getState(a,3) && state.getState(a,3) == state.getState(a,4)) if (state.getState(a,4) > 0) return state.getState(a,4);
		}
		if (state.getState(2,2) == state.getState(3,3) && state.getState(3,3) == state.getState(4,4)) if (state.getState(3,3) > 0) return state.getState(3,3);
		if (state.getState(4,2) == state.getState(3,3) && state.getState(3,3) == state.getState(2,4)) if (state.getState(3,3) > 0) return state.getState(3,3);
		return NONE;
	}
	
	public GamePlay(int plaId, int plbId, boolean turnSet, int depth, int overrun) {
		turn = turnSet ? 1 : 0;

		if ((plaType = plaId) < 10);// isBot[0] = false; else isBot[0] = true;
		if ((plbType = plbId) < 10);// isBot[1] = false; else isBot[1] = true;
		state = new GameState();
		
		whoseMove = HUMAN_MOVE;
		if(plaType != HUMAN && !turnSet) {
			whoseMove = COMPUTER_MOVE;
		} else if(plbType != HUMAN && turnSet) {
			whoseMove = COMPUTER_MOVE;
		}
		
		if (turnSet) {
			int temp = plaType;
			plaType = plbType;
			plbType = temp;
		}
		
		// TODO network interface here
		//plaIn = null; zakomentowalem bo mnie warningi wkurzaly (tomek)
		//plbIn = null;
		
		if (plaType == ALFABETAZABOJCA) plaOut = new AlgAlfaBetaSimple(depth); 
		else if (plaType == POWERBETA) plaOut = new AlgPowerBeta(depth); 
		else if (plaType == UNDERTTAKER) plaOut = new UnderTTaker(depth, overrun); 
		else if (plaType == MADDOG) plaOut = new AlgMadDog(depth, overrun); 
		else if (plaType == EMDA) plaOut = new EptinessMurderess(depth, overrun);
		else if (plaType == NET) plaOut = new NetPlayer(this);
		else plaOut = null;
		if (plbType == ALFABETAZABOJCA) plbOut = new AlgAlfaBetaSimple(depth); 
		else if (plbType == POWERBETA) plbOut = new AlgPowerBeta(depth); 
		else if (plbType == UNDERTTAKER) plbOut = new UnderTTaker(depth, overrun); 
		else if (plbType == MADDOG) plbOut = new AlgMadDog(depth, overrun); 
		else if (plbType == EMDA) plbOut = new EptinessMurderess(depth, overrun);
		else if (plbType == NET) plbOut = new NetPlayer(this);
		else plbOut = null;
		
	}
	
	/**
	 * Switches the turn
	 */
	public Move[] turnSwitch() {
		turn = (turn + 1) % 2;
		Move[] moves;
		if (turn == 1 && plbType != HUMAN) {
			System.out.println("Waiting for player 2...");
			moves = plbOut.makeMove(state, turn + 1);			
		} else if (turn == 0 && plaType != HUMAN) {
			System.out.println("Waiting for player 1...");
			moves = plaOut.makeMove(state, turn + 1);						
		} else return null;
		return moves;
	}
	
	/**
	 * gets whose turn is now
	 * @return turn
	 */
	public int getTurn() {
		return turn;
	}
	
	/**
	 * @return state.
	 */
	public GameState getCurrentState() {
		return state;
	}
	
	public void setCurrentState(GameState state) {
		this.state = state;
	}
	
	public void forceTransition(Move mvi) {
		state = state.trans(mvi);
	}
}
